package com.example.springbootexamples.util;

import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class MathUtil {

    /*public static Double compare(Double pre,Double post){
        Double r;
        if(pre != null && post != null){
            if(pre==0){
                if(post>0){
                    r=1d;
                }else if(post<0){
                    r=-1d;
                }else {
                    r=0d;
                }
            }else {
                r=(post-pre)/pre;
            }
        }else if(pre == null && post== null){
            r=0d;
        }else{
            if(pre == null){
                 if(post==0){
                     r=0d;
                 }else if(post>0){
                     r=1d;
                 }else {
                     r=-1d;
                 }
            }
            if(post== null){
                if(post==0){
                    r=0d;
                }else if(post<0){
                    r=-1d;
                }else{
                    r=1d;
                }
            }
        }
        return r;
    }*/

    public static Double div(Double pre,Double post){
        return div(pre,post,2);
    }

    public static Double div(Double pre,Double post,Integer scale){
        if(scale==null){
            scale=2;
        }
        Double d;
        if((pre != null && pre!=0 && post != null)){
            d= (post-pre)/pre;
        }else{
            if((pre == null || pre==0)){
                if(post==null || post==0){
                    d= 0d;
                }else {
                    d= post>0?1d:-1d;
                }
            }else {
                if(post==null){
                    d=-1d;
                }else {
                    d=(post-pre)/pre;
                }
            }
        }
        d=d*100;
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(scale, RoundingMode.HALF_UP);
        d=bd.doubleValue();
        return d;
    }

    @Test
    public void t1(){

        Double pre=102d;
        Double post=20d;
        System.out.println(div(pre,post));
        System.out.println("========");
        double d=234.12300d;
        BigDecimal bigDecimal=new BigDecimal(Double.toString(d));
        bigDecimal=bigDecimal.setScale(-1,BigDecimal.ROUND_HALF_UP);
        System.out.println(bigDecimal.doubleValue());

    }


    public static Double zhanbi(Double pre,Double post){
        return ratio(pre,post);
    }

    public static Double tongbi(Double pre,Double post){
        return yoy(pre,post);
    }

    /**
     * 占比
     * @param pre
     * @param post
     * @return
     */
    public static Double ratio(Double pre,Double post){
        if(pre != null && pre !=0 && post != null){
            return post/pre*100;
        }
        return null;
    }

    /**
     * 同比
     * @param pre
     * @param post
     * @return
     */
    public static Double yoy(Double pre,Double post){
        if(pre != null && post != null){
            return ratio(pre,post-pre);
        }
        return null;
    }

    public static Double get(Double d){

        if(d==null) return null;
        BigDecimal bigDecimal=new BigDecimal(d.toString());
        return bigDecimal.setScale(3,RoundingMode.HALF_UP).doubleValue();
    }


    @Test
    public void t2(){

        Double pre=300d;
        Double post=100d;

        System.out.println(ratio(pre,post));
        System.out.println(yoy(pre,post));
        System.out.println(get(yoy(pre,post)));
    }

    @Test
    public void t3(){

        class User{
            private Integer total;
            private Integer err;
            private Double duration;

            public Integer getTotal() {
                return total;
            }

            public void setTotal(Integer total) {
                this.total = total;
            }

            public Integer getErr() {
                return err;
            }

            public void setErr(Integer err) {
                this.err = err;
            }

            public Double getDuration() {
                return duration;
            }

            public void setDuration(Double duration) {
                this.duration = duration;
            }
        }


        //  error num
        //  error rate
        //  error rate avg
        //  error rate ratio




    }

    public static double getDouble2(double d,int scale){
        BigDecimal bigDecimal=new BigDecimal(d);
        return bigDecimal.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    public static double getDouble1(double d,int scale){
        double p=Math.pow(10,scale);
        return Math.round(d*p)/p;
    }


    @Test
    public void t(){

        double d=83.335;














        
        System.out.println(getDouble1(d,2));
        System.out.println(getDouble2(d,2));

    }

}
